App.MainApp.module('buttonbarModule', function(buttonbarModule){
	var _modulename = this.moduleName;
	this.startWithParent = false;
	// private
	var _layout = null;
	// View for Layout
   var MainView = Marionette.ItemView.extend({
		tagName: 'div',
		initialize: function() {},
		events: {
			'click .buttonbar-button': 'button_click'
		},
		button_click: function(e) {
			App.MainApp.vent.trigger('buttonbarModule:button_click', {
				id: $(e.currentTarget).attr('rel')
			});
		}
   });
	// Controller for Layout
	var Controller = Marionette.Controller.extend({
		initialize: function(options){
			if (App.debug) console.log(this.moduleName + ' Controller initialized');
			this.region = options.region
		},
		show: function(options) {
			if (App.debug) console.log(this.moduleName + ' Controller show invoked');
			var main_view = new MainView({
				id: options.id,
				template: '#' + options.template
			});
			this.region.show(main_view);
		},
		render: function(options) {
			var self = this;
			$('#'+ options.id + ' > .buttonbar-button').button();
			if (App.debug) console.log(this.moduleName + ' Controller render invoked');
		},
	});
	
	buttonbarModule.addInitializer(function (options) {
		var defaults = {
			id: 'buttonbar-layout',
			region: App.MainApp.buttonbarRegion
		};
		options = _.extend(defaults, options);
		if (App.debug) console.log(_modulename + ' addInitializer');
		buttonbarModule.Controller = new Controller({
			region: options.region
		});
		buttonbarModule.Controller.show(options);
		buttonbarModule.Controller.render(options);
   });
});

App.MainApp.buttonbarModule.on("before:start", function(){
	if (App.debug) console.log(this.moduleName + ' before:start');
});

App.MainApp.buttonbarModule.on("start", function(){
	if (App.debug) console.log(this.moduleName + ' start');
});

